<?php
namespace Home\Controller;
use Common\Controller\CommonController;
use Org\Util\Wechat;
class MsgController extends CommonController {
	public $wx = null; 
	
	public function _initialize(){  
		$this->wx = new Wechat(C('options'));
	}
	
	function index(){
		$this->display('index');
	}
	
	/**
	 * [research_list 问卷调查列表]
	 * author  [lzm]
	 * time    2016年7月5日
 	 * @return [array] [description]
	 */
	public function content_list(){
		//$where="`title` neq '客服评价'";
		$count = M('notice_content')->where('title!="客服评价"')->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		$list=M('notice_content')->where('title!="客服评价"')->limit($Page->firstRow.','.$Page->listRows)->order('ctime desc')->select();
		$arr=array('page'=>$show,'list'=>$list);
		$this->assign('arr',$arr);
		$this->display('index');

	}
	//添加问卷调查页面
	public function add_content_index(){
		$this->display('add_content');
	}
	

	//添加内容
	public function add_content(){
		$user=session('LG_USER');
		$content=M('notice_content');
		//接收参数
		$title=I('title');
		$con=trim(I('content'));
		$ctime=time();
		$type=I('type');
		$data=array(
			'title'=>$title,
			'content'=>$con,
			'type'=>$type,
			'ctime'=>$ctime,
			'people'=>$user['username'],
			);
		//print_r($data);exit('@#$%');
		$res_add=$content->data($data)->add();
		if (false !== $res_add) {
			 $this->redirect('Msg/content_list');
		}else{
			  error('操作失败',$_SERVER['HTTP_REFERER']);
		}
		
	}



	//删除内容
	public function del_content(){
		$id = I('id');
		
		$content = M('notice_content');
		$user = session('LG_USER');
		$username = $user['username'];
		//只能删除自己的
		$name=I('name');
		if($username!='admin'){
			if ($name!=$username) {
				error('操作失败,只能删除自己创建的哦',$_SERVER['HTTP_REFERER']);
			}
		}
		//删除调查表数据
		$res_del = $content->where('id='.$id)->delete();
		
		if (false !== $res_del && false !== $result_del) {
		    $this->redirect('Msg/content_list');
		}else{
			 error('操作失败',$_SERVER['HTTP_REFERER']);
		}
	}



	//群发列表
	public function news(){
		$user=session('LG_USER');
		//当前登录用户的id
		$id=$user['id'];
		
		if ($id!=1) {
			$list_r=M('rbac_manager')->where('id="'.$id.'"')->find();
			$name=$list_r['username'];
			$where=array('send_name'=>$name);
			$count = M('notice')->where($where)->count();// 查询满足要求的总记录数
			$Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show       = $Page->show();// 分页显示输出
			$list=M('notice')->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('sendtime desc')->select();
			$arr=array('page'=>$show,'list'=>$list);
			$this->assign('list',$arr);
		}else{
			
			$count = M('notice')->count();// 查询满足要求的总记录数
			$Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show       = $Page->show();// 分页显示输出
			$list=M('notice')->limit($Page->firstRow.','.$Page->listRows)->order('sendtime desc')->select();
			$arr=array('page'=>$show,'list'=>$list);
			$this->assign('list',$arr);
		}

		$this->display('news');
	}





	//查看发送的小区
	public function apt_list(){
		$id=I('id');
		$notice=M('notice');
		$list=$notice->where('id="'.$id.'"')->find();
		$for_apt=explode(',',$list['for_apt']);
		//print_r($for_apt);exit;

		foreach ($for_apt as $key => $value) {
			$name[]=M('apt')->field('name')->where('id="'.$value.'"')->find();
			$array=$name;
		}

		$this->assign('name',$array);
		$this->display('apt_list');
	}



	//查看发送的客户
	public function people_list(){
		$id=I('id');
		$notice=M('notice');
		$list=$notice->where('id="'.$id.'"')->find();
		$for_people=explode(',',$list['for_people']);
		//print_r($for_apt);exit;
		foreach ($for_people as $key => $value) {
			$name[]=M('customer')->field('name')->where('id="'.$value.'"')->find();
			$array=$name;
		}

		$this->assign('name',$array);
		$this->display('people_list');
	}



	//发送消息页面	
	public function add_question(){
		$user=session('LG_USER');
		//当前登录用户的id
		$id=$user['id'];
		
		if ($id!=1) {
			$list_sc=M('rbac_manager')->where('id="'.$id.'"')->find();

			$sc_id=$list_sc['sc'];//P($sc_id);
			//属于自己的服务中心
			$list=M('sc')->where('id="'.$sc_id.'"')->select();

			//查找属于自己的小区
			$arr=M('apt')->where('sid="'.$sc_id.'"')->field('id,name,sid')->select();
			//查找自己小区的用户
			$user_list=M('customer')->select();
		}else{
			//所有服务中心
			$list=M('sc')->select();
			//所有小区
			$arr=M('apt')->field('id,name,sid')->select();
			//所有注册之后的用户
			$user_list=M('customer')->select();
		}		
		//内容模板
		$content=M('notice_content')->where('content!="客服评价"')->select();
		//print_r($content);exit;
		$this->assign('con',$content);
		$this->assign('list',$list);
		$this->assign('arrlist',$arr);
		$this->assign('user_list',$user_list);
		$this->display('add_question');
	}

	//服务中心=>小区ajax联动请求
	public function ajax(){
		$check=I('id');
		$arr=explode(",",$check);
		foreach ($arr as $key => $value) {
			$id=$value;
			$list=M('apt')->where('sid="'.$id.'"')->field('id,name,sid')->select();
			$array[]=$list;
		}

		foreach ($array as $key => $val) {
			foreach ($val as $ke => $va) {
					$data[]=$va;
			}
		}
		$this->ajaxReturn(array('s'=>"success",'m'=>$data));
	}

	

	//小区=》用户ajax联动请求
	public function ajax_user(){
		$check=I('id');
		$arr=explode(",",$check);
		foreach ($arr as $key => $value) {
			$id=$value;
			$list=M('customer')->where('plot="'.$id.'"')->field('id,note_name,name,phone,plot')->select();
			$array[]=$list;
		}

		foreach ($array as $key => $val) {
			foreach ($val as $ke => $va) {
					$data[]=$va;
			}
		}
		//print_r($data);exit('####');
		$this->ajaxReturn(array('s'=>"success",'m'=>$data));
	}



	/**
	 * *添加发送记录到数据库
	 * @return [type] [description]
	 */
	public function send_news(){
		$user=session('LG_USER');
		$username=$user['username'];
		
		$type=I('type');
		$area=trim(I('area'));
		if($area==2){
			if($user['id']!=1){
				error('只有超级管理员才可以发送全部用户,请尝试群发或者单发，谢谢',$_SERVER['HTTP_REFERER']);
		    }
		}
		$obj=I('obj');
		$content=trim(I('content'));
		$notice=M('notice');

		//群发
		if ($area=="0") {
			$sc=I('sc');
			$apt=implode(',',I('apt'));
			$data=array( 
			'type'=>$type,
			'sendtype'=>$area,
			'send_name'=>$username,
			'obj'=>$obj,
			'for_apt'=>$apt,
			'contid'=>$content,
			'sendtime'=>time(),
			);
			if (empty($username)) {
				error('发送人不能为空',$_SERVER['HTTP_REFERER']);
			}
			$res_add=M('notice')->data($data)->add();
			if (false == $res_add){
				 error('发送失败',$_SERVER['HTTP_REFERER']);
			}else{
				$this->forea($res_add);
				$this->redirect('Msg/news');
			}

		}
		//单个发送
		if ($area=="1") {
			$sc=I('sc');
			$apt=implode(',',I('apt'));
			$people=implode(',',I('user'));
			$data=array( 
			'type'=>$type,
			'sendtype'=>$area,
			'send_name'=>$username,
			'obj'=>$obj,
			'for_apt'=>$apt,
			'contid'=>$content,
			'for_people'=>$people,
			'sendtime'=>time(),
			);
			if (empty($username)) {
				error('发送人不能为空',$_SERVER['HTTP_REFERER']);
			}
			//print_r($data);exit('@@@');
			$res_add=M('notice')->data($data)->add();
			if (false == $res_add){
				 error('发送失败',$_SERVER['HTTP_REFERER']);
			}else{
				$this->forea($res_add);
				$this->redirect('Msg/news');

			}
		}
		//全部用户
		if ($area=="2") {
			$sc=I('sc');
			$data=array( 
			'type'=>$type,
			'sendtype'=>$area,
			'send_name'=>$username,
			'contid'=>$content,
			'obj'=>$obj,
			'for_apt'=>0,
			'for_people'=>0,
			'sendtime'=>time(),
			);
			if (empty($username)) {
				error('发送人不能为空',$_SERVER['HTTP_REFERER']);
			}
			//print_r($data);exit('@@@');
			$res_add=M('notice')->data($data)->add();
			if (false == $res_add){
				 error('发送失败',$_SERVER['HTTP_REFERER']);
			}else{
				$this->forea($res_add);
				$this->redirect('Msg/news');

			}
		}
	}


	//删除问题
	public function del_news(){
		$user=session('LG_USER');
		$username=$user['username'];
		if ($username!="admin") {
			error('只能是超级管理员才可以删除哦',$_SERVER['HTTP_REFERER']);
		}
		$id=I('id');
		$res=M('notice')->where('id="'.$id.'"')->find();
		$rid=$res['rid'];
		$res_del=M('notice')->where('id="'.$id.'"')->delete();
		if (false !== $res_del) {
		    $this->redirect('Msg/news',array('id' => $rid));
		}else{
			exit('fail');
		    $this->redirect('Msg/news');
		}
	}


	//获取模板消息列表
	public function template_list(){
		$access_token=$this->wx->checkAuth();
		$url="https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=".$access_token."";
		$res=file_get_contents($url);
		$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
		foreach ($result as $key => $value) {
			/*print_r($value);*/
			foreach ($value as $k => $v) {
				//print_r($v);
				$title[] = $v['title'];
			    $array=$title;
			}
			
		}
        //$title = $result['title'];
		//print_r($access_token);exit('@@@');
		$this->assign('template',$array);
		$this->display('');
	}

	//根据发送范围,判断小区里的openid
	public function forea($res_add){
		$list=M('notice')->where('id="'.$res_add.'"')->find();
		$sendtype=$list['sendtype'];
		//群发
		if ($sendtype==0) {
			$apt=explode(',',$list['for_apt']);
			//print_r($apt);exit;
			foreach ($apt as $key => $value) {
				$open=M('customer')->where('plot="'.$value.'"')->select();
				foreach ($open as $k => $v) {
					$openid=$v['openid'];
					$res=M('customer')->where('openid="'.$val.'"')->data(array('m_status'=>0,))->save();
					$this->send_template($openid,1);
				}
			}
		}
		//单个发送
		if ($sendtype==1) {
			$people=explode(',',$list['for_people']);
			foreach ($people as $key => $value) {
				$open=M('customer')->where('id="'.$value.'"')->find();
				$openid=$open['openid'];
				$res=M('customer')->where('openid="'.$openid.'"')->data(array('m_status'=>0,))->save();
				$this->send_template($openid,1);
			}
		}
		//全部微信用户
		if ($sendtype==2) {
			$data=M('customer')->select();
			foreach ($data as $key => $value) {
				$openid=$value['openid'];
				$res=M('customer')->where('openid="'.$openid.'"')->data(array('m_status'=>0,))->save();
				$this->send_template($openid,1);
			}
		}
		
	}

	//发送模板消息
	public function send_template($openid,$type){
		$list=M('customer')->where('openid="'.$openid.'"')->find();
		$list_apt=M('apt')->where('id="'.$list['plot'].'"')->find();
		$name=$list_apt['name'];
		if ($type==1) {
			$weObj = new Wechat(C('options'));  
			$data =  array(
				"touser" =>	$openid,
				"template_id" =>	"Yu4ci-e8xVZ90dVZ1WwcUm0gJSCyy_rE0XXZ4Mc8feY",
				"url" =>	"http://sunwayuchoice.com/index.php/Gj/index/openid/".$openid,
				"topcolor" =>	"#FF0000",
				"data" => array( 
						'first'	=>	array(
							"value" =>	"您有新的信息通知",
							"color" =>	"#ff0000"
						),
						'keyword1'	=>	array(
							"value" =>	$name,
							"color" =>	"#173177"
						),
						'keyword2'	=>	array(
							"value" =>	'消息通知',
							"color" =>	"#173177"
						),
						'keyword3'	=>	array(
							"value" =>	"您好，您有一条新消息，请到个人中心查看。",
							"color" =>	"#173177"
						),
						'remark'	=>	array(
							"value" =>	"尚云优家Uchoice",
							"color" =>	"#173177"
						)
					)
			);
			//P($data,false);
			$st = $weObj->sendTemplateMessage($data);
		}
		if ($type==2) {
			$data=
			$weObj = new Wechat(C('options'));  
			$data =  array(
				"touser" =>	$openid,
				"template_id" =>	"Yu4ci-e8xVZ90dVZ1WwcUm0gJSCyy_rE0XXZ4Mc8feY",
				"url" =>	"http://sunwayuchoice.com/index.php/Gj/index/openid/".$openid,
				"topcolor" =>	"#FF0000",
				"data" => array( 
						'first'	=>	array(
							"value" =>	"您有新的信息通知",
							"color" =>	"#ff0000"
						),
						'keyword1'	=>	array(
							"value" =>	$name,
							"color" =>	"#173177"
						),
						'keyword2'	=>	array(
							"value" =>	'消息通知',
							"color" =>	"#173177"
						),
						'keyword3'	=>	array(
							"value" =>	"您好，您有一条新消息，请到个人中心查看。",
							"color" =>	"#173177"
						),
						'remark'	=>	array(
							"value" =>	"尚云优家Uchoice",
							"color" =>	"#173177"
						)
					)
			);
			//P($data,false);
			$st = $weObj->sendTemplateMessage($data);
		}
		if ($type==3) {
		$weObj = new Wechat(C('options'));  
			$data =  array(
				"touser" =>	$openid,
				"template_id" =>	"Yu4ci-e8xVZ90dVZ1WwcUm0gJSCyy_rE0XXZ4Mc8feY",
				"url" =>	"http://sunwayuchoice.com/index.php/Gj/index/openid/".$openid,
				"topcolor" =>	"#FF0000",
				"data" => array( 
						'first'	=>	array(
							"value" =>	"您有新的信息通知",
							"color" =>	"#ff0000"
						),
						'keyword1'	=>	array(
							"value" =>	$name,
							"color" =>	"#173177"
						),
						'keyword2'	=>	array(
							"value" =>	'消息通知',
							"color" =>	"#173177"
						),
						'keyword3'	=>	array(
							"value" =>	"您好，您有一条新消息，请到个人中心查看。",
							"color" =>	"#173177"
						),
						'remark'	=>	array(
							"value" =>	"尚云优家Uchoice",
							"color" =>	"#173177"
						)
					)
			);
			//P($data,false);
			$st = $weObj->sendTemplateMessage($data);
		}
		
      /*  $ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$tmpInfo = curl_exec($ch);
		var_dump($tmpInfo);*/
	}
	
}